package com.example.demo.controller;

import com.example.demo.model.MessageInfo;
import com.example.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Hsu琛君珩
 * @ClassName:MessageController
 * @date 2023-11-30
 * @apiNote
 * @Version: v1.0
 */

@RestController
@RequestMapping("/message")
public class MessageController {

    //注入对象可以 属性@Autowired 注入，也可 构造方法 或者 setter
    @Autowired
    private MessageService messageService;

    @RequestMapping("/publish")
    public Boolean publishMessage(MessageInfo messageInfo){
        //进行参数校验
        //三个任意一个为空
        if(!StringUtils.hasLength(messageInfo.getFrom())
                || !StringUtils.hasLength(messageInfo.getTo())
                || !StringUtils.hasLength(messageInfo.getMessage())){
            return false;
        }
        //添加留言
        messageService.addMessage(messageInfo);
        return true;
    }

    @RequestMapping("/getMessageInfo")
    public List<MessageInfo> getMessage(){
        //直接返回留言
        return messageService.getMessageInfo();
    }
}
